#include "../src/EventFileDriver.h"
#include "../src/EventTable.h"

#include <stdio.h>

int
main(void)
{
	EventTable *e_table;
	char	    buf[20];
	int	    status;

	e_table = event_table_new();
	event_file_driver_set_file(EVENT_FILE_DRIVER(e_table),
				   "eventtable.efd");

	status = event_file_driver_loader(EVENT_FILE_DRIVER(e_table));
	if (status != EVENT_FILE_DRIVER_SUCCESS) {
		printf("打开文件失败！(code: %d)\n尝试创建文件 ......\n",
		       status);
		status = event_file_driver_create(EVENT_FILE_DRIVER(e_table));
		if (status != EVENT_FILE_DRIVER_SUCCESS) {
			printf("创建文件错误！(code: %d)\n", status);
			return status;
		} else {
			printf("创建文件成功！\n");
		}
	} else {
		printf("打开文件成功！\n");
	}

	__event_table_print(e_table);

	EventString *str;
	euint32	     event_id;
	for (int i = 0; i < 10; ++i) {
		sprintf(buf, "Title%d", i);
		str	 = event_string_new(buf);
		event_id = event_table_set_event(e_table, 0, 1000000 + i,
						 6677 + i, str, str);
		printf("已自动添加一个事件 EventID<%07d>\n", event_id);
		event_object_unref(str);
	}

	__event_table_print(e_table);

	status = event_file_driver_dump(EVENT_FILE_DRIVER(e_table));
	if (status == EVENT_FILE_DRIVER_SUCCESS)
		printf("写入文件成功！\n");
	else
		printf("写入文件出错！(code: %d)\n", status);

	event_object_unref(e_table);
	return 0;
}
